package com.gk.javaservice.util.cmdUtil;

import java.io.*;

public class RunBat {

    public static void setbat() {
        String cmd =  "@echo off\n" +
                "REM get cmd pid\n" +
                "set TempFile=%TEMP%\\sthUnique.tmp\n" +
                "wmic process where (Name=\"wmic.exe\" AND CommandLine LIKE \"%%%TIME%%%\") get ParentProcessId /value | find \"ParentProcessId\" >%TempFile%\n" +
                "set /P _string=<%TempFile%\n" +
                "set _pid=%_string:~16%\n" +
                "echo %_pid%"+
                "\n" +
                "D:\\server\\bats\\ffmpeg.exe -i rtmp://58.200.131.2:1935/livetv/hunantv -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv  -f flv rtmp://192.168.10.93:1935/myapp/53.flv"+
                "\n" +
                "exit";
        String url = "D:\\server\\bats\\1.bat";
        FileWriter fw = null;
        try {
            // 生成bat文件
            fw = new FileWriter(url);
            fw.write(cmd);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            // 运行bat文件
            // System.out.println("运行bat文件");
           // Runtime.getRuntime().exec("cmd.exe   /C   start  "+url);
            Process process = Runtime.getRuntime().exec("cmd.exe   /C   start  "+url);
            InputStream in = process.getInputStream();
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            while ((line = br.readLine()) != null) {
                // System.out.println(line);
                process.destroy();
            }
            in.close();
            //process.waitFor();
            // System.out.println("执行成功");
        } catch (Exception e) {
            // System.out.println("执行失败");
        }
    }




    public static void main(String []args) throws IOException {
        setbat();



       // Runtime.getRuntime().exec("cmd.exe   /C   start   D:\\server\\bats\\1.bat");
    }

}
